package com.neusoft.bizcore.notice.notification.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.notice.notification.dto.NoticeTemplateDTO;
import com.neusoft.bizcore.notice.notification.service.NoticeTemplateService;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "通知模板")
@RestController
@RequestMapping(value = "/api/w/noticeTemplate")
public class NoticeTemplateController {

    @Autowired
    private NoticeTemplateService noticeTemplateService;

    @ApiOperation("查询所有通知模板")
    @GetMapping
    public ResultListDTO<NoticeTemplateDTO> index(final Searchable searchable) {
        final List<NoticeTemplateDTO> noticeTemplateDTOs = this.noticeTemplateService.index(searchable);
        return ResultListDTO.success(noticeTemplateDTOs);
    }

    @ApiOperation("创建通知模板")
    @PostMapping
    public ResultDTO<NoticeTemplateDTO> create(@RequestBody final NoticeTemplateDTO dto) {
        final NoticeTemplateDTO result = this.noticeTemplateService.create(dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("通知模板详情")
    @GetMapping("/{id}")
    public ResultDTO<NoticeTemplateDTO> details(@PathVariable final Long id) {
        final NoticeTemplateDTO result = this.noticeTemplateService.details(id);
        return ResultDTO.success(result);
    }

    @ApiOperation("删除通知模板")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.noticeTemplateService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("修改通知模板")
    @PutMapping("/{id}")
    public ResultDTO<NoticeTemplateDTO> update(@PathVariable final Long id,
            @RequestBody final NoticeTemplateDTO dto) {
        final NoticeTemplateDTO result = this.noticeTemplateService.update(id, dto);
        return ResultDTO.success(result);
    }

    @ApiOperation("分页查询所有通知模板")
    @GetMapping("/p")
    public PageResultDTO<NoticeTemplateDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.noticeTemplateService.search(pageable, searchable));
    }

    @ApiOperation("下载默认模板")
    @GetMapping("/download")
    public ResultDTO<Void> download(final HttpServletResponse response) {
        this.noticeTemplateService.downloadDefaultTemplate(response);
        return ResultDTO.success();
    }

}
